package com.hdsx.restful.glzt.roadline.xzqh.rest;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.springframework.stereotype.Service;

import com.hdsx.restful.glzt.roadline.lxxx.bean.Xzqh;
import com.hdsx.restful.glzt.roadline.xzqh.mapper.XzqhMapper;
import com.hdsx.restful.glzt.roadline.xzqh.service.XzqhService;
import com.hdsx.restful.util.CheckEmpty;
import com.hdsx.restful.util.EasyUI;


@Path("/xzqh")
@Service
public class XzqhRestServer {
	@Resource(name="xzqhServiceImpl")
	private XzqhService service;
	@Resource
	private XzqhMapper mapper;
	@GET
	@Path("/list")
	@Produces(MediaType.APPLICATION_JSON)
	public EasyUI<Xzqh> getList(@QueryParam("lxdm") String lxdm,@QueryParam("xzdj")String xzdj,
			@QueryParam("xzqhbm")String xzqhbm,@QueryParam("name") String name,@QueryParam("sxxfx")String sxxfx_string,
			@QueryParam("page") int page,@QueryParam("rows") int rows) throws Exception{
		EasyUI<Xzqh> easyUI=new EasyUI<Xzqh>(page,rows);
		Integer sxxfx = null;
		try{
			sxxfx = Integer.parseInt(sxxfx_string);
		}catch(Exception e){
			sxxfx = null;
		}
		xzqhbm = CheckEmpty.subXzqhbm(xzqhbm);
		Map<String, Object> map = easyUI.getParameter();
		map.put("lxdm", lxdm);
		map.put("xzdj", xzdj);
		map.put("xzqhbm", xzqhbm);
		map.put("sxxfx", sxxfx);
		map.put("name", name);
		int count =service.queryCount(map);
		List<Xzqh> results = service.queryList(map);
		if(!CheckEmpty.isEmptyString(xzqhbm)){
			for(Xzqh xzqh : results){
				xzqh.setXzqhbm(xzqhbm);
			}
		}
		easyUI.setTotal(count);
		easyUI.setRows(results);
		return easyUI;
	}
	
	@GET
	@Path("/auto")
	@Produces(MediaType.APPLICATION_JSON)
	public List<String> pienext(@QueryParam("field")String field,@QueryParam("tableName")String tableName,@QueryParam("xzdj")String xzdj,
			@QueryParam("conditions")String conditions,@QueryParam("conditionValues")String conditionValues){
		
		String[] condition=conditions.split(",");
		String[] conditionValue=conditionValues.split(",");
		String request=field.replaceAll(",", "||'-'||");
		String sql="SELECT * FROM (select Z,row_number() over (order by Z ) as RN from(select distinct "+request+" AS Z from "+tableName+"  ";
		
		
		if(conditionValues!=null&&!conditionValues.isEmpty()){
			sql+=" WHERE ";
			for(int i=0;i<condition.length&&i<conditionValue.length;i++)
			{	
				if(conditionValue[i]!=null&&!conditionValue[i].isEmpty()){
					if((xzdj.equals("Z")||xzdj.equals("Y")||xzdj.equals("C")||xzdj.equals("X"))&&(condition[i].equals("lxdm")||condition[i].equals("lddm"))&&conditionValue[i].length()<=4){
						sql+=" " +"substr("+condition[i]+",0,4)"+" like '%' || '"+conditionValue[i]+"' || '%' AND";
					}else{
						sql+=" " +condition[i]+" like '%' || '"+conditionValue[i]+"' || '%' AND";
					}
				}
			}
			sql=sql.substring(0, sql.lastIndexOf("AND"));
		}
		if(xzdj!=null&&!xzdj.equals("")){
			if(xzdj.equals("Z")||xzdj.equals("Y")||xzdj.equals("C")){
				sql+="AND lddm like '"+xzdj+"' || '%' ";
			}else{
				sql+="AND lxdm like '"+xzdj+"' || '%' ";
			}
		}
		sql+="))where RN >0 and RN <=15";
		System.out.println(sql);
		List<String> list=mapper.autoComplete(sql);
		return list;
	}
}
